<?php

namespace app\admin\model;

use think\Model;


class CategoryIncome extends Model
{

    

    

    // 表名
    protected $name = 'category_income';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    







    public function category()
    {
        return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    //券金额
    public static function quan_amount($category_id, $quan_price)
    {
        $date_year_month=date('Y-m',time());
        $category_income = self::where('category_id', $category_id)->where('date_year_month',$date_year_month)->find();
        if($category_income){
            $category_income->quan_amount = $category_income->quan_amount + $quan_price;
            $category_income->save();
        }else{
            $category_income = new self();
            $category_income->category_id = $category_id;
            $category_income->quan_amount = $quan_price;
            $category_income->date_year_month = $date_year_month;
            $category_income->save();
        }
    }

    //微信充值金额
    public static function wechat_recharge_amount($category_id, $wechat_recharge_amount)
    {
        $date_year_month=date('Y-m',time());
        $category_income = self::where('category_id', $category_id)->where('date_year_month',$date_year_month)->find();
        if($category_income){
            $category_income->wechat_recharge_amount = $category_income->wechat_recharge_amount + $wechat_recharge_amount;
            $category_income->save();
        }else{
            $category_income = new self();
            $category_income->category_id = $category_id;
            $category_income->wechat_recharge_amount = $wechat_recharge_amount;
            $category_income->date_year_month = $date_year_month;
            $category_income->save();
        }
    }

    //微信充值金额
    public static function order_pay_amount($category_id, $order_pay_amount)
    {
        $date_year_month=date('Y-m',time());
        $category_income = self::where('category_id', $category_id)->where('date_year_month',$date_year_month)->find();
        if($category_income){
            $category_income->order_pay_amount = $category_income->order_pay_amount + $order_pay_amount;
            $category_income->save();
        }else{
            $category_income = new self();
            $category_income->category_id = $category_id;
            $category_income->order_pay_amount = $order_pay_amount;
            $category_income->date_year_month = $date_year_month;
            $category_income->save();
        }
    }


    //退款金额
    public static function refund_amount($category_id, $refund_amount)
    {
        $date_year_month=date('Y-m',time());
        $category_income = self::where('category_id', $category_id)->where('date_year_month',$date_year_month)->find();
        if($category_income){
            $category_income->refund_amount = $category_income->refund_amount + $refund_amount;
            $category_income->save();
        }else{
            $category_income = new self();
            $category_income->category_id = $category_id;
            $category_income->refund_amount = $refund_amount;
            $category_income->date_year_month = $date_year_month;
            $category_income->save();
        }
    }



}
